Imports CAPA_Entidad
Imports System.Data
Imports System.Data.SqlClient
Imports CAPA_Utilitarios
Public Class DocumentoAlmacenDetallePickeadorUbicacionDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function registrarDocumentoAlmacenDetallePickeadorUbicacion(ByVal dadpu As DocumentoAlmacenDetallePickeadorUbicacion) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "USP_RegistrarDocumentoAlmacenDetallePickeadorUbicacion"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", dadpu.CIA)
                .Parameters.AddWithValue("@SUC", dadpu.SUC_Codigo)
                .Parameters.AddWithValue("@TDOC", dadpu.TDOC_Codigo)
                .Parameters.AddWithValue("@DCACOD", dadpu.DCA_Codigo)
                .Parameters.AddWithValue("@DADPCOD", dadpu.DADP_Codigo)
                .Parameters.AddWithValue("@DADPUCOD", dadpu.DADPU_Codigo)
                .Parameters.AddWithValue("@ALM", dadpu.ALM_Codigo)
                .Parameters.AddWithValue("@ARTCOD", dadpu.ART_Codigo)
                .Parameters.AddWithValue("@UBICOD", dadpu.UBI_Codigo)
                .Parameters.AddWithValue("@DADPUCANT", dadpu.DADPU_Cantidad)
                .Parameters.AddWithValue("@DADPUCPICK", dadpu.DADPU_CantPick)
                .Parameters.AddWithValue("@ACT", dadpu.DADPU_Activo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function actualizarDocumentoAlmacenDetallePickeadorUbicacion(ByVal dadpu As DocumentoAlmacenDetallePickeadorUbicacion) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "USP_actualizarDocumentoAlmacenDetallePickeadorUbicacion"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", dadpu.CIA)
                .Parameters.AddWithValue("@SUC", dadpu.SUC_Codigo)
                .Parameters.AddWithValue("@TDOC", dadpu.TDOC_Codigo)
                .Parameters.AddWithValue("@DCACOD", dadpu.DCA_Codigo)
                .Parameters.AddWithValue("@DADPCOD", dadpu.DADP_Codigo)
                .Parameters.AddWithValue("@DADPUCOD", dadpu.DADPU_Codigo)
                .Parameters.AddWithValue("@ALM", dadpu.ALM_Codigo)
                .Parameters.AddWithValue("@ARTCOD", dadpu.ART_Codigo)
                .Parameters.AddWithValue("@UBICOD", dadpu.UBI_Codigo)
                .Parameters.AddWithValue("@DADPUCANT", dadpu.DADPU_Cantidad)
                .Parameters.AddWithValue("@DADPUCPICK", dadpu.DADPU_CantPick)
                .Parameters.AddWithValue("@@DADPUEPICK", dadpu.DADPU_ErrorPicking)
                .Parameters.AddWithValue("@ACT", dadpu.DADPU_Cantidad)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function obtenerListadoDocumentoAlmacenDetallePickeadorUbicacion(ByVal codCIA As String, ByVal codSUC As String, ByVal codTDOC As String, ByVal codDCA As String, ByVal codDADP As String) As DataSet
        Dim ds As New DataSet
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "SELECT * FROM VW_DocumentoAlmacenDetallePickeadorUbicacion " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDOC AND DCA_Codigo = @DCA " & _
                                  "AND DADP_Codigo = @DADP"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDOC", codTDOC)
                .Parameters.AddWithValue("@DCA", codDCA)
                .Parameters.AddWithValue("@DADP", codDADP)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            adapter.Fill(ds)
        Catch ex As Exception
            ds = Nothing
        Finally
            con.Close()
        End Try

        Return ds
    End Function

    Function obtenerDocumentoAlmacenDetallePickeadorUbicacion(ByVal codCIA As String, ByVal codSUC As String, ByVal codTDOC As String, ByVal codDCA As String, ByVal codDADP As String, ByVal codDADPU As String) As DocumentoAlmacenDetallePickeadorUbicacion
        Dim dadpu As New DocumentoAlmacenDetallePickeadorUbicacion("", "", "", "", "", "", "", "", "", "0", "0", "0", "", "", "", "", "")
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "SELECT * FROM VW_DocumentoAlmacenDetallePickeadorUbicacion " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDOC AND DCA_Codigo = @DCA " & _
                                  "AND DADP_Codigo = @DADP AND DADPU_Codigo = @DADPU"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDOC", codTDOC)
                .Parameters.AddWithValue("@DCA", codDCA)
                .Parameters.AddWithValue("@DADP", codDADP)
                .Parameters.AddWithValue("@DADPU", codDADPU)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count > 0 Then
                dadpu = New DocumentoAlmacenDetallePickeadorUbicacion(ds.Tables(0).Rows(0)("CIA").ToString.Trim, ds.Tables(0).Rows(0)("SUC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDOC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCA_Codigo").ToString.Trim, _
                                                                      ds.Tables(0).Rows(0)("DADPU_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("ALM_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DADP_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("ART_Codigo").ToString.Trim, _
                                                                      ds.Tables(0).Rows(0)("UBI_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DADPU_Cantidad").ToString.Trim, ds.Tables(0).Rows(0)("DADPU_CantIng").ToString.Trim, ds.Tables(0).Rows(0)("DADPU_CantPick").ToString.Trim, _
                                                                      ds.Tables(0).Rows(0)("DADPU_FechaAsig").ToString.Trim, ds.Tables(0).Rows(0)("DADPU_FechaInicio").ToString.Trim, ds.Tables(0).Rows(0)("DADPU_FechaFin").ToString.Trim, ds.Tables(0).Rows(0)("DADPU_ErrorPicking").ToString.Trim, ds.Tables(0).Rows(0)("DADPU_Activo").ToString.Trim)
            End If
        Catch ex As Exception
            dadpu = Nothing
        Finally
            con.Close()
        End Try

        Return dadpu
    End Function

    Sub actualizarCantidadIngresada(ByVal tipoDOC As String, ByVal cantidad As Integer, ByVal codigo As String)
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "UPDATE documento_almacen_detalle_pickeador_ubicacion " & _
                                  "SET DADPU_CantIng = DADPU_CantIng + @cantidad " & _
                                  "WHERE DADPU_Codigo = @codigo " & _
                                  "AND TDOC_Codigo = @tipoDoc"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@cantidad", cantidad)
                .Parameters.AddWithValue("@codigo", codigo)
                .Parameters.AddWithValue("@tipoDoc", tipoDOC)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try
    End Sub

    Function generarCodigoDADPU(ByVal tdocodigo As String) As Integer
        Dim numero As Integer = 0
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "SELECT COUNT(*) AS Contar FROM Documento_Almacen_Detalle_Pickeador_Ubicacion WHERE TDOC_Codigo = '" & tdocodigo & "'"

            Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, con)
            Dim ds As New DataSet
            adapter.Fill(ds)

            numero = CInt(ds.Tables(0).Rows(0)("Contar").ToString.Trim) + 1
        Catch ex As Exception
            numero = -1
        Finally
            con.Close()
        End Try

        Return numero
    End Function

    Function retomarUbicacionesPickeador(ByVal codCIA As String, ByVal codSUC As String, ByVal codTipo As String, ByVal codDCA As String, ByVal codDAP As String) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "USP_RetomarPicking"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandText = query
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDO", codTipo)
                .Parameters.AddWithValue("@DCA", codDCA)
                .Parameters.AddWithValue("@DAP", codDAP)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

End Class
